package com.my.util;


import java.util.HashMap;

public final class Tags {

    public static String[] splitString(String s, char c) {
        final char[] chars = s.toCharArray();
        int num_substrings = 1;
        for (final char x : chars) {
            if (x == c) {
                num_substrings++;
            }
        }
        final String[] result = new String[num_substrings];
        final int len = chars.length;
        int start = 0;  // starting index in chars of the current substring.
        int pos = 0;    // current index in chars.
        int i = 0;      // number of the current substring.
        for (; pos < len; pos++) {
            if (chars[pos] == c) {
                result[i++] = new String(chars, start, pos - start);
                start = pos + 1;
            }
        }
        result[i] = new String(chars, start, pos - start);
        return result;
    }

    public static long parseLong(String replace) {
        return 1L;
    }

    public static void parse(HashMap tags, String cmd) {
        final String[] kv = splitString(cmd, '=');
        if (kv.length != 2 || kv[0].length() <= 0 || kv[1].length() <= 0) {
            throw new IllegalArgumentException("invalid tag: " + cmd);
        }
        if (kv[1].equals(tags.get(kv[0]))) {
            return;
        }
        if (tags.get(kv[0]) != null) {
            throw new IllegalArgumentException("duplicate tag: " + cmd
                    + ", tags=" + tags);
        }
        tags.put(kv[0], kv[1]);
    }

    public static boolean looksLikeInteger(String value) {
        final int n = value.length();
        for (int i = 0; i < n; i++) {
            final char c = value.charAt(i);
            if (c == '.' || c == 'e' || c == 'E'){
                return false;
            }
        }
        return true;
    }

    public static boolean fitsInFloat(String value) {
        final double d = Double.parseDouble(value);
        return ((float) d) == d;
    }
}
